-- NewEquipStrengthenView

local Armature = require("app.public.avatar.Armature")
local IconRender = require("app.widget.renders.IconRender")

local ATTR_NUM = 3 -- UI上装备属性条目数

local ClassRef = app.mvc.createPanelClass(...)

function ClassRef.create(node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewEquipStrengthenUi))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self:getTimeline():gotoFrameAndPause(0)

	self.super.init(self, data)

	display.setCascadeOpacityEnabled( self._ui["Panel/Panel_tips"], true )

	self._ui["Panel/Button_close"]:addClickEventListener(function()
			executeFunc(self._onPanelHandler, "equipStrengthen_closePanel")
		end)

	-- self._ui["Panel/Panel_up/Button_explain"]:addClickEventListener(function()
	-- 		-- 说明
	-- 	end)

	self._strengthBtn = self._ui["Panel/Panel_up/Button_s"]
	self._strengthBtn:addClickEventListener(function() -- 强化确定
			if self._needItem and
				self._myItemNum and -- 存在消耗？
				self._needItem.amount <= self._myItemNum then -- 数量足够？
			 	executeFunc(self._onPanelHandler, "equipStrengthen_strength", self._data.uid)
			else
				self:getTimeline():gotoFrameAndPlay(0, false)
			end
		end)

	self._strengthTenBtn = self._ui["Panel/Panel_up/Button_all"]
	self._strengthTenBtn:addClickEventListener(function() -- 强化确定
			if self._needItem and
				self._myItemNum and -- 存在消耗？
				self._needItem.amount <= self._myItemNum then -- 数量足够？
			 	executeFunc(self._onPanelHandler, "equipStrengthen_strength_ten", self._data.uid)
			else
				self:getTimeline():gotoFrameAndPlay(0, false)
			end
		end)

	self._equipName = self._ui["Panel/title"]

	self._equipIcon = IconRender.create( self._ui["Panel/Panel_mid/icon_1"] )
	self._neededIcon = IconRender.create( self._ui["Panel/Panel_mid/icon_2"] )

	self._neededItemName = self._ui["Panel/Panel_mid/Text_2"]
	self._neededNum = self._ui["Panel/Panel_mid/num"]

	self._successRate = self._ui["Panel/Panel_up/num_gold"]
	self._successBar = self._ui["Panel/Panel_up/bar/LoadingBar"]
end

-- data = equipData
function ClassRef:refresh()
	local strengthenLevel = self._data:getStrengthLv()
	local name = self._data:getName()
	if strengthenLevel > 0 then
		name = string.format("%s +%d", name, strengthenLevel)
	end
	self._equipName:setString( name )
	self._equipName:setTextColor( Enums.QualityColor[ self._data.quality ] )

	self._equipIcon:setData( self._data )
	self._equipIcon:setClickEnable( false )

	if self._data:isStrengthMax() then -- 已经强化到当前最大等级
		self._ui["Panel/Panel_mid/Panel_ami"]:setVisible( false )
		self._ui["Panel/Panel_bg/bg06"]:setVisible( false )
		self._ui["Panel/Panel_bg/bg07"]:setVisible( false )
		self._ui["Panel/Panel_bg/line_01"]:setVisible( false )
		self._ui["Panel/Panel_up"]:setVisible( false )
		self._neededIcon:setVisible( false )
		self._neededItemName:setVisible( false )
		self._neededNum:setVisible( false )
		self._ui["Panel/Panel_mid/data_2"]:setVisible( false )
		self._ui["Panel/Panel_mid/Text_Max"]:setVisible( true )
	else
		self._ui["Panel/Panel_bg/bg06"]:setVisible( true )
		self._ui["Panel/Panel_bg/bg07"]:setVisible( true )
		self._ui["Panel/Panel_bg/line_01"]:setVisible( true )
		self._ui["Panel/Panel_up"]:setVisible( true )
		self._neededIcon:setVisible( true )
		self._neededItemName:setVisible( true )
		self._neededNum:setVisible( true )
		self._ui["Panel/Panel_mid/data_2"]:setVisible( true )
		self._ui["Panel/Panel_mid/Text_Max"]:setVisible( false )

		self:refreshItem()

		local successRate = 0 -- 现在只有2个状态：成功/失败、默认失败
		self._successRate:setVisible( true )

		self._ui["Panel/Panel_up/Text_1"]:setVisible( true )
		self._ui["Panel/Panel_up/Text_MaxLv"]:setVisible( false )

		-- 显示概率
		local equipGrid = UD:getEquipGridByPos(self._data:getPos())
		if equipGrid then
			successRate = math.floor( equipGrid:getUpgradeProbability() / 100 )
			if successRate > 100 then successRate = 100 end
		end

		self._successRate:setString( successRate.. "%" )
		self._successBar:setPercent( successRate )

		if 0 < successRate then -- 0<successRate -->> successRate=100
			self:getTimeline():play("over", true)
			self._ui["Panel/Panel_mid/Panel_ami"]:setVisible( true )
		else
			self._ui["Panel/Panel_mid/Panel_ami"]:setVisible( false )
		end
	end

	-- 当前强化等级属性
	local leftDataPanel = self._ui["Panel/Panel_mid/data_1"]:getChildByName("Panel")
	local strengthenLvTitleText = leftDataPanel:getChildByName("lv_tlt")
	if 0 < strengthenLevel then
		strengthenLvTitleText:setString( L("lua_code_text_529") .. strengthenLevel )
	else
		strengthenLvTitleText:setString( L("lua_code_text_447") )
	end
	for i = 1, ATTR_NUM do
		leftDataPanel:getChildByName("data_tlt_"..i):setVisible( false )
		leftDataPanel:getChildByName("data_num_"..i):setVisible( false )
	end
	local curLvAttrList = self._data:getStrengthAttr()
	local curLvAttrNum = 0
	for k,v in pairs( curLvAttrList ) do
		curLvAttrNum = curLvAttrNum + 1

		leftDataPanel:getChildByName("data_tlt_"..curLvAttrNum):setVisible( true )
		leftDataPanel:getChildByName("data_num_"..curLvAttrNum):setVisible( true )

		local propConf =  GD:getPropConfig( tonumber( k ) )
		leftDataPanel:getChildByName("data_tlt_"..curLvAttrNum):setString( L(propConf.name) )

		local _, formatAttrNum = UD:formatAttr( propConf.ID, v )
		leftDataPanel:getChildByName("data_num_"..curLvAttrNum):setString( formatAttrNum )
	end


	-- 下一级强化等级属性
	local rightDataPanel = self._ui["Panel/Panel_mid/data_2"]:getChildByName("Panel")
	rightDataPanel:getChildByName("lv_tlt"):setString( L("lua_code_text_529") .. strengthenLevel + 1 )
	for i = 1, ATTR_NUM do
		rightDataPanel:getChildByName("data_tlt_"..i):setVisible( false )
		rightDataPanel:getChildByName("data_num_"..i):setVisible( false )
	end
	local nextLvAttrList = self._data:getStrengthAttr( strengthenLevel + 1 )
	local nextLvAttrNum = 0
	for k,v in pairs( nextLvAttrList ) do
		nextLvAttrNum = nextLvAttrNum + 1

		rightDataPanel:getChildByName("data_tlt_"..nextLvAttrNum):setVisible( true )
		rightDataPanel:getChildByName("data_num_"..nextLvAttrNum):setVisible( true )

		local propConf =  GD:getPropConfig( tonumber( k ) )
		rightDataPanel:getChildByName("data_tlt_"..nextLvAttrNum):setString( L(propConf.name) )

		local _, formatAttrNum = UD:formatAttr( propConf.ID, v )
		rightDataPanel:getChildByName("data_num_"..nextLvAttrNum):setString( formatAttrNum )
	end

	for i = nextLvAttrNum, 1, -1 do
		local leftAttr = leftDataPanel:getChildByName("data_num_"..i)
		local rightAttr = rightDataPanel:getChildByName("data_num_"..i)

		local curLvAttr = tonumber( leftAttr:getString() or 0 )
		local nextLvAttr = tonumber( rightAttr:getString() or 0 )

		if nextLvAttr > curLvAttr then -- 属性更强？
			rightDataPanel:getChildByName("data_num_"..i):setColor( cc.c3b( 187, 254, 0 ) )
		else
			rightDataPanel:getChildByName("data_num_"..i):setColor( leftDataPanel:getChildByName("data_num_"..i):getColor() )
		end
	end
end

function ClassRef:refreshItem()
	local strengthenLevel = self._data:getStrengthLv()
	local nextConf = GD:getEquipRridEnhanceConfByStrengthenLevel( strengthenLevel + 1 )
	-- 升级消耗
	local strengthCostStr = nextConf["cost" .. self._data:getPos()] -- 大类|ID|数量;
	self._needItem = UD:parseRewardToBagItem(strengthCostStr)
	self._neededIcon:setData( self._needItem )
	self._neededIcon:setTipEnable( true )
	self._neededItemName:setString( L("lua_code_text_277") .. " " .. self._needItem:getName(true) )

	self._myItemNum = UD:getNumByRenderData(self._needItem)
	self._neededNum:setString( self._myItemNum .. "/" .. self._needItem.amount )
	
	if self._needItem.amount <= self._myItemNum then -- 数量足够
		self._neededNum:setColor( display.COLOR_WHITE )
		display.setBtnTouchEnable( self._strengthBtn, true)
		display.setBtnTouchEnable( self._strengthTenBtn, true)
	else
		self._neededNum:setColor( display.COLOR_RED )
		display.setBtnTouchEnable( self._strengthBtn, false)
		display.setBtnTouchEnable( self._strengthTenBtn, false)
	end
end

function ClassRef:updateItem(tid)
	if tid and self._needItem and tid == self._needItem:getId() then
		self:refreshItem()
	end
end

function ClassRef:playStrengthSuccessAmi()
	if not self._successArmature then
		local params = {
			armatureName = "QH1",
			path = Res.EquipStrengthSuccessAmi
		}
		local armature = Armature.create(params)
		armature:setPosition(0, 0)
		self._ui.amiNode:addChild(armature)
		self._successArmature = armature
	end
	self._successArmature:play("QH1", 1)
end

function ClassRef:playStrengthFailAmi()
	if not self._failArmature then
		local params = {
			armatureName = "QH2",
			path = Res.EquipStrengthFailAmi
		}
		local armature = Armature.create(params)
		armature:setPosition(0, 0)
		self._ui.amiNode:addChild(armature)
		self._failArmature = armature
	end
	self._failArmature:play("QH2", 1)

	local aniNode = self._aniFailParticle
	if not aniNode then
		aniNode = cc.CSLoader:createNode(Res.EquipStrengthParticle, true)
		aniNode:setPosition(0, 0)
		self._ui.amiNode:addChild(aniNode)
		self._aniFailParticle = aniNode
	end

	local timeline = aniNode:getTimeline()
	local aniParticle = aniNode:findChild("Panel/Particle")
	timeline:gotoFrameAndPlay(0, false)
	aniParticle:resetSystem()
end

function ClassRef:registerOnOperate( cb )
	self._onPanelHandler = cb
end

return ClassRef
